using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Entities;
namespace DAL
{
  public   class tbDulichDAL
    {
       private SqlConnection cn = null;
       public tbDulichDAL()
       {
           cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
           cn.Open();
          
       }
       public void Insert(tbDulich  dl)
       {
           SqlCommand cm = new SqlCommand("Du_lich_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Madden", dl.Madden  );
           cm.Parameters.AddWithValue("@Masp", dl.Masp  );
           cm.ExecuteNonQuery();
       }
       public void Update(tbDulich  dl)
       {
           SqlCommand cm = new SqlCommand("Du_lich_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Madul", dl.Madul );
           cm.Parameters.AddWithValue("@Madden", dl.Madden );
           cm.Parameters.AddWithValue("@Masp", dl.Masp);
           cm.ExecuteNonQuery();
       }
       public void Delete(int iD)
       {
           SqlCommand cm = new SqlCommand("Du_lich_Delete", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Madul", iD );
           cm.ExecuteNonQuery();
       }
       public tbDulichCollection  Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("Du_lich_Get", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
        tbDulichCollection  dlCollection = new tbDulichCollection  ();
         while (dr.Read())
         {
             tbDulich  dl= new tbDulich ();
          dl.Madul  = int.Parse(dr.GetValue(0).ToString());
             dl .Madden  = int.Parse (dr.GetValue(1).ToString());
             dl.Masp  = int.Parse(dr.GetValue(2).ToString());
             dlCollection.Add(dl);
         }
         dr.Close();
         totalRecords = int.Parse(
         cm.Parameters["@TotalRecords"].Value.ToString());
         return dlCollection ;
       }
    }
}
